$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        um:null,
        init:function () {
            var $this = this;
            this.um = UM.getEditor('myEditor',{
                imageUrl:baseUrl+"frontend/article/uploadImgs", //处理图片上传的接口
                imageFieldName:"upfile", //上传图片的表单的name
                toolbar:['source | undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor | removeformat |',
                    'insertorderedlist insertunorderedlist | selectall cleardoc paragraph | fontfamily fontsize' ,
                    '| justifyleft justifycenter justifyright justifyjustify |',
                    'link unlink | image | accessory ',
                    '| horizontal  formula'],
                imagePath : ''
            });
            this.form = new xhForm($('#xh_form'));
            this.form.parse();
            this.form.bindRequired($('#bt_modal_confirm', parent.document));
            if (info) {
                this.form.fill(info);
                this.um.ready(function() {//编辑器初始化完成再赋值
                    if (info['mText']) {
                        $this.um.setContent(info['mText']);
                    }
                });
                if (info['sCoverUrl']) {
                    $('[name=pcCover]').before('<img src="'+info['sCoverUrl']+'" style="height: 100px;width: 162px"><br>');
                }
                if (info['sCoverMobile']) {
                    $('[name=mobileCover]').before('<img src="'+info['sCoverMobile']+'" style="height: 100px;width: 135px;"><br>');
                }
            };
            this.listener();
        },
        listener:function () {
            var $this = this;

            //保存
            $('#bt_modal_confirm', parent.document).click(function () {
                $this.funObj().commitForm();
            });


        },
        funObj:function () {
            var $this = this;
            return {
                commitForm:function () {
                    var formData = new FormData($('#xh_form')[0]);
                    $.ajax({
                        url: baseUrl+"article/saveNews",
                        type: "POST",
                        data: formData,
                        processData: false,
                        contentType: false,
                        complete: function(XMLHttpRequest){
                            xhExtra.setBtnNormal($('#bt_modal_confirm', parent.document));
                            var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
                            if (result.code == 200) {
                                window.top.xhTop.showSuccess(result.msg);
                                window.top.xhTop.closeModal();
                                window.top.xhTop.getActiveFrame().obj.dataGird.reload();
                            } else {
                                window.top.xhTop.showError('错误',result.error);
                            }
                        }
                    });
                }
            }
        }
    }
};